import { getStream } from '../Core/connect'
import { Object3D } from './Object3D'
import * as MathUtils from '../Math/MathUtils'
import { Vector3 } from '../Math/Vector3'


export class DataSmith extends Object3D {
    readonly type: string
    constructor(name: string) {
        super({name: name, type: 'DataSmith'})
        this.type = 'DataSmith'
    }

    setUrl(url: string) {
        const that = this
        const sessionId = MathUtils.generateUUID()
        const list: Array<object> = []
        return new Promise(function (resolve) {
            const stream = getStream()
            const str = {
                dataSmithFunction: 'setDataSmithUrl',
                ids: `${that.type},${that.name},${that.uuid}`, 
                position: `${that.position.x},${that.position.y},${that.position.z}`,
                rotate: `${that.rotate.x},${that.rotate.y},${that.rotate.z}`,
                size: `${that.size.x},${that.size.y},${that.size.z}`,
                url: url,
                sessionId: sessionId
            }
            stream.emitUIInteraction(str)
            const dataSmithReady = (data: any) => {
                const returnData = JSON.parse(data)
                if(returnData.sessionId === sessionId){
                    list.push(returnData.data)
                    if(returnData.state === 'ready'){
                    resolve(list)
                        // console.log('ready', list)
                        stream.removeResponseEventListener(`dataSmithReady${that.uuid}`)
                    }
                }
            }
            stream.addResponseEventListener(`dataSmithReady${that.uuid}`, dataSmithReady)
        })
    }

    setLonLatPosition(position:Vector3){
        this.position = position
        const stream = getStream()
        const str = {dataSmithFunction: 'setLonLatPosition', ids: `${this.uuid}`,position:`${this.position.x},${this.position.y},${this.position.z}`}
        stream.emitUIInteraction(str)
    }

    getModelByName(name: string) {
        const that = this
        const sessionId = MathUtils.generateUUID()
        return new Promise(function (resolve) {
            const stream = getStream()
            const str = {dataSmithFunction: 'getDataSmithByName', ids: `${that.uuid}`, nodeName:`${name}`, sessionId: sessionId}
            stream.emitUIInteraction(str)
            const getGlbPosition = (data: any) => {
                const returnData = JSON.parse(data)
                console.log('模型位置',data)
                if(returnData.sessionId === sessionId){
                    console.log('模型位置',returnData)
                    stream.removeResponseEventListener(`dataSmithPosition${that.uuid}`)
                    resolve(returnData)
                }
            }
            stream.addResponseEventListener(`dataSmithPosition${that.uuid}`, getGlbPosition)
        })
    }

    setClick() {
        const stream = getStream()
        const str = {dataSmithFunction: 'setClick', ids: `${this.uuid}`}
        const uuid = this.uuid
        stream.emitUIInteraction(str)
        const getDataSmithClick = (data: string) => {
            const returnData = JSON.parse(data)
            if (returnData.sessionId === uuid) {
                console.log('hahaha', returnData)
            }
        }
        stream.addResponseEventListener(`dataSmithClick${this.uuid}`, getDataSmithClick)
    }
}